<template>
<div>
<van-cell-group inset><!--分组-->
  <van-field label="用户名" placeholder="请输入用户名" v-model="login.name"></van-field><!--文本框-->
  <van-field label="密码" placeholder="请输入密码" type="password" v-model="login.password"></van-field>
  <van-button type="primary" v-on:click="dologin" size="large">登录</van-button>
  <van-button type="info" size="large" v-on:click="toregist">如果没有账号，请注册</van-button>
</van-cell-group>


</div>
</template>

<script>
  import {removeStorageToken,getStorageUser,setStorageUser} from "@/utils/storage.js"//导入
import { Dialog } from "vant";
import request, { getUrl } from "../../utils/request";
  export default{
    name:"login",
    data(){
      return{
        login:{//存储登录信息
          name:"Admin",
          password:"123123"
        }
      }
    },
    methods:{
      dologin:function(){
        if(this.login.name==="" || this.login.password===""){
          this.$toast("用户名或密码为空！");
          return;
        }
        request({//发起request请求
          url:getUrl("User/doLogin"),//URL
          method:"post",//请求方法
          headers: {
                  'Content-Type': 'application/x-www-form-urlencoded'
          },
          data:{//Post传data，get请求传parma
            uName:this.login.name,
            pwd:this.login.password
          }
        }).then(res=>{//成功回调
            if(res.data.code==1){//用户名密码错误
              Dialog.alert({//弹出对话框
                title: '登录提示',
                message: '用户名密码错误',
              }).then(() => {
                // on close
              });
            }else if(res.data.code==0){//登录成功
                setStorageUser(res.data.data);//存储当前登录用户
                this.$router.push("/");//路由跳转
            }else{//系统错误！
              showDlg('错误提醒','网络错误！')
            }
        }).catch(error=>{//失败回调
          console.log(error)
        })
      },
      toregist:function(){//跳转到注册
        this.$router.push("/register");//路由跳转
      }
    }
  }
</script>

<style >
  .goto {
    display: block;
    margin-top: 25px;
    text-align: center;
  }
</style>
